<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ResetFaker;
use App\Dealer;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class FakeDealersController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('各区域虚拟经销商设置')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Dealer);
        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->disableCreateButton();
        $grid->disablePagination();
        $grid->disableFilter();
        $grid->disableExport();
        $grid->tools(function ($tools) {
            $tools->append(new ResetFaker());
        });
        $grid->model()->where('is_fake', 1);
        $grid->id('Id');
        $grid->region('营销总部');
        $grid->goods_1($GLOBALS['goods'][0]->name)->editable();
        $grid->goods_2($GLOBALS['goods'][1]->name)->editable();
        $grid->goods_3($GLOBALS['goods'][2]->name)->editable();
        $grid->goods_4($GLOBALS['goods'][3]->name)->editable();
        $grid->goods_5($GLOBALS['goods'][4]->name)->editable();
        $grid->goods_6($GLOBALS['goods'][5]->name)->editable();
        $grid->column('订货量合计(万箱)')->display(function (){
            return set_decimal($this->getAllGoodsCount());
        });
        $grid->column('金额合计(万元)')->display(function (){
            return set_decimal($this->getAllMoneyCount());
        });
        $grid->disableRowSelector();
        $grid->footer(function ($v) {
            $headers = ['合计', '营销总部', $GLOBALS['goods'][0]->name, $GLOBALS['goods'][1]->name, $GLOBALS['goods'][2]->name,
                $GLOBALS['goods'][3]->name, $GLOBALS['goods'][4]->name, $GLOBALS['goods'][5]->name, '订货量合计(万箱)', '金额合计(万元)'];
            $dealers = Dealer::where('is_fake', 1)->get();
            $goodsRows = $this->getGoodsData($dealers);
            $goodsCount = array_values($goodsRows);
            $sum = array_sum($goodsCount);
            $arr1 = ['订货合计','全国'];
            $arr1 = array_merge($arr1, $goodsCount, [$sum], [0]);
            $moneyRows = $this->getMoneyData($dealers);
            $moneyCount = array_values($moneyRows);
            $sumMoney = array_sum($moneyCount);
            $arr2 = ['金额合计', '全国'];
            $arr2 = array_merge($arr2, $moneyCount, [0], [$sumMoney]);
            $rows = [
                $arr1, $arr2,
            ];

            $table = new Table($headers, $rows);
            return $table;
        });
        return $grid;
    }

    protected function getGoodsData($dealers)
    {
        $goods1 = 0;
        $goods2 = 0;
        $goods3 = 0;
        $goods4 = 0;
        $goods5 = 0;
        $goods6 = 0;
        foreach ($dealers as $dealer) {
            $goods1 += $dealer->goods_1;
            $goods2 += $dealer->goods_2;
            $goods3 += $dealer->goods_3;
            $goods4 += $dealer->goods_4;
            $goods5 += $dealer->goods_5;
            $goods6 += $dealer->goods_6;
        }
        return [
            'goods_1' => set_decimal($goods1),
            'goods_2' => set_decimal($goods2),
            'goods_3' => set_decimal($goods3),
            'goods_4' => set_decimal($goods4),
            'goods_5' => set_decimal($goods5),
            'goods_6' => set_decimal($goods6),
        ];
    }

    protected function getMoneyData($dealers)
    {
        $goods1 = 0;
        $goods2 = 0;
        $goods3 = 0;
        $goods4 = 0;
        $goods5 = 0;
        $goods6 = 0;
        foreach ($dealers as $dealer) {
            $goods1 += $dealer->goods_1 * $GLOBALS['goods'][0]->price;
            $goods2 += $dealer->goods_2 * $GLOBALS['goods'][1]->price;
            $goods3 += $dealer->goods_3 * $GLOBALS['goods'][2]->price;
            $goods4 += $dealer->goods_4 * $GLOBALS['goods'][3]->price;
            $goods5 += $dealer->goods_5 * $GLOBALS['goods'][4]->price;
            $goods6 += $dealer->goods_6 * $GLOBALS['goods'][5]->price;
        }
        return [
            'goods_1' => set_decimal($goods1),
            'goods_2' => set_decimal($goods2),
            'goods_3' => set_decimal($goods3),
            'goods_4' => set_decimal($goods4),
            'goods_5' => set_decimal($goods5),
            'goods_6' => set_decimal($goods6),
        ];
    }


    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Dealer::findOrFail($id));

        $show->id('Id');
        $show->region('Region');
        $show->market('Market');
        $show->company('Company');
        $show->username('Username');
        $show->password('Password');
        $show->name('Name');
        $show->market_level('Market level');
        $show->goods_1('Goods 1');
        $show->goods_2('Goods 2');
        $show->goods_3('Goods 3');
        $show->goods_4('Goods 4');
        $show->goods_5('Goods 5');
        $show->created_at('Created at');
        $show->updated_at('Updated at');
        $show->goods_6('Goods 6');


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Dealer);

        $form->display('region', '营销总部');

        $form->number('goods_1', $GLOBALS['goods'][0]->name);
        $form->number('goods_2', $GLOBALS['goods'][1]->name);
        $form->number('goods_3', $GLOBALS['goods'][2]->name);
        $form->number('goods_4', $GLOBALS['goods'][3]->name);
        $form->number('goods_5', $GLOBALS['goods'][4]->name);
        $form->number('goods_6', $GLOBALS['goods'][5]->name);
        return $form;
    }

    public function resetFaker()
    {
        $fakeDealers = Dealer::where('is_fake', 1)->get();
        $data = [
            'goods_1' => 0,
            'goods_2' => 0,
            'goods_3' => 0,
            'goods_4' => 0,
            'goods_5' => 0,
            'goods_6' => 0,
        ];
        foreach ($fakeDealers as $dealer) {
            $dealer->where('id', $dealer->id)->update($data);
        }
        return ['success' => true];
    }
}
